﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using BLL.System;

namespace MIS
{
    public partial class Page_Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            bool relogin = false;
            if (Request.QueryString["relogin"] != null)  relogin = true;
            if (!IsPostBack)
            {
                InitData();

                if (relogin == false)
                {   //auto login
                    this.pnlLoading.Visible  = true;
                    string DomainAndName = User.Identity.Name;
                    string[] infoes = DomainAndName.Split('\\');
                    string userDomainName = string.Empty, userName = string.Empty;

                    if (infoes.Length > 1)
                    {
                        userDomainName = infoes[0];
                        userName = infoes[1];
                    }

                    string strVerUserAD = DomainAndName.ToLower();
                    UserBO ubo = new UserBO();
                    if (!string.IsNullOrEmpty(strVerUserAD) && ubo.IsAccountExist(strVerUserAD))
                    {
                        Framework.Caching.CacheHelper<DataTable>.Remove(new Framework.Caching.CacheKey(typeof(DataTable), "function"));
                        Response.Redirect("Default.aspx");
                    }
                    else
                    {
                        this.pnlLoading.Visible = false;
                        this.errorMsg.InnerText = string.Format("系统中不存在{0}帐号！", strVerUserAD);
                        this.errorMsg.Visible = true;
                        return;
                    }

                }

            }

        }

        /// <summary>
        /// 保存COOKIE
        /// </summary>
        private void setCookie() {

            switch (RadioButtonList1.SelectedIndex)
            {
                case 0:
                    Response.Cookies["login_userid"].Value = txtID.Value;
                    Response.Cookies["login_userid"].Expires = DateTime.Now.AddDays(7);
                    Response.Cookies["login_userpasswd"].Value = CommonLib.EncryptionLib.GetEncryptor(txtPassword.Text , true);
                    Response.Cookies["login_userpasswd"].Expires = DateTime.Now.AddDays(7);
                    Response.Cookies["login_useroption"].Value = RadioButtonList1.SelectedIndex.ToString();
                    Response.Cookies["login_useroption"].Expires = DateTime.Now.AddDays(30);
                    break;
                case 1:
                    Response.Cookies["login_userid"].Value = txtID.Value;
                    Response.Cookies["login_userid"].Expires = DateTime.Now.AddDays(7);
                    Response.Cookies["login_userpasswd"].Value = CommonLib.EncryptionLib.GetEncryptor(txtPassword.Text, true);
                    Response.Cookies["login_userpasswd"].Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies["login_useroption"].Value = RadioButtonList1.SelectedIndex.ToString();
                    Response.Cookies["login_useroption"].Expires = DateTime.Now.AddDays(30);
                    break;
                case 2:
                    Response.Cookies["login_userid"].Value = txtID.Value;
                    Response.Cookies["login_userid"].Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies["login_userpasswd"].Value = CommonLib.EncryptionLib.GetEncryptor(txtPassword.Text, true);
                    Response.Cookies["login_userpasswd"].Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies["login_useroption"].Value = RadioButtonList1.SelectedIndex.ToString();
                    Response.Cookies["login_useroption"].Expires = DateTime.Now.AddDays(30);
                    break;
            }

        
        }

        /// <summary>
        /// 从COOKIE中初始化输入
        /// </summary>
        private void InitData()
        {
            HttpCookie get_USERID_cookie = Request.Cookies["login_userid"];
            HttpCookie get_USERPASSWD_cookie = Request.Cookies["login_userpasswd"];
            HttpCookie get_USEROPTION_cookie = Request.Cookies["login_useroption"];


            if (get_USERID_cookie != null)
            {
                txtID.Value = get_USERID_cookie.Value.ToString();
            }
            if (get_USERPASSWD_cookie != null)
            {
                txtPassword.Attributes.Add("value",  CommonLib.EncryptionLib.GetDecryptor(get_USERPASSWD_cookie.Value.ToString(), true));
            }
            if (get_USEROPTION_cookie != null)
            {
                RadioButtonList1.SelectedIndex = Convert.ToInt16(get_USEROPTION_cookie.Value);
            }

        }

        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            string username = txtID.Value;
            string pwd = txtPassword.Text;
            UserBO ubo = new UserBO();
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(pwd))
            {
                this.errorMsg.InnerText = "用户名或密码不能为空！";
                this.errorMsg.Visible = true;
                return;
            }
            if (ubo.CheckPwd(username,pwd))
            {
                setCookie();
                Framework.Caching.CacheHelper<DataTable>.Remove(new Framework.Caching.CacheKey(typeof(DataTable), "function"));
                Response.Redirect("Default.aspx");
            }
            else
            {
                this.errorMsg.InnerText = "用户名或密码错误！";
                this.errorMsg.Visible = true;
            }

        }
    }
}